package com.example.sixtrainingdemo.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.sixtrainingdemo.R;
import com.example.sixtrainingdemo.entity.Photo;
import com.example.sixtrainingdemo.entity.RoundPhoto;

import java.util.List;

public class RoundPhotoAdapter extends RecyclerView.Adapter<RoundPhotoAdapter.ViewHolder> {

   private static final int ITEM_TYPE_PHOTO = 1;
   private static final int ITEM_TYPE_ADD = 2;
   private List<RoundPhoto> mItems;
   private OnPhotoListener mOnPhotoListener;

   public RoundPhotoAdapter(List<RoundPhoto> list) {
      mItems = list;
   }

   public List<RoundPhoto> getPhotoList() {
      return mItems;
   }

   @SuppressLint("NotifyDataSetChanged")
   public void addRoundPhoto(RoundPhoto roundphoto) {
      if (mItems != null && mItems.size() > 0) {
         mItems.add(mItems.size() - 1, roundphoto);
         notifyDataSetChanged();
      }
   }

   @NonNull
   @Override
   public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
      View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.goods_photo_item, null);
      return new ViewHolder(view);
   }

   @Override
   public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
      if (getItemViewType(position) == ITEM_TYPE_ADD) {
         holder.roundphoto.setImageResource(R.drawable.add_photo);
         holder.delete.setVisibility(View.GONE);
         holder.roundphoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               mOnPhotoListener.onAdd();
            }
         });
      } else {
         RoundPhoto roundphoto = mItems.get(position);
         holder.roundphoto.setImageBitmap(roundphoto.getBitmap());
         holder.delete.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onClick(View v) {
               mOnPhotoListener.onDelete(position);
               mItems.remove(position);
               notifyDataSetChanged();
            }
         });
      }
   }

   @Override
   public int getItemCount() {
      return mItems.size();
   }

   @Override
   public int getItemViewType(int position) {
      if (mItems != null && mItems.size() - 1 == position) {
         return ITEM_TYPE_ADD;
      }
      return ITEM_TYPE_PHOTO;
   }

   public static class ViewHolder extends RecyclerView.ViewHolder {
      ImageView roundphoto;
      ImageView delete;

      public ViewHolder(@NonNull View itemView) {
         super(itemView);
         roundphoto = itemView.findViewById(R.id.goods_photo);
         delete = itemView.findViewById(R.id.delete_photo);
      }
   }

   public void setPhotoListener(OnPhotoListener listener) {
      mOnPhotoListener = listener;
   }

   public interface OnPhotoListener{
      void onAdd();
      void onDelete(int position);
   }
}
